package com.bookstore.back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bookstore.back.entity.Bestseller;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * 畅销榜单Mapper接口
 *
 * @author 程序猿_Ti
 * @since 2025-07-23
 */
@Mapper
public interface BestsellerMapper extends BaseMapper<Bestseller> {

    /**
     * 分页查询畅销榜单，包含图书信息
     */
    @Select("<script>" +
            "SELECT b.*, " +
            "       bk.title, bk.author, bk.publisher, bk.price, bk.cover, bk.sales " +
            "FROM bestseller b " +
            "LEFT JOIN book bk ON b.book_id = bk.id " +
            "WHERE 1=1 " +
            "<if test='params.categoryId != null and params.categoryId != \"\"'>" +
            "  AND b.category_id = #{params.categoryId} " +
            "</if>" +
            "<if test='params.period != null and params.period != \"\"'>" +
            "  AND b.period = #{params.period} " +
            "</if>" +
            "<if test='params.title != null and params.title != \"\"'>" +
            "  AND bk.title LIKE CONCAT('%', #{params.title}, '%') " +
            "</if>" +
            "ORDER BY b.rank ASC" +
            "</script>")
    IPage<Map<String, Object>> selectBestsellerWithBook(Page<?> page, @Param("params") Map<String, Object> params);

}
